package org.sny7.openOfflineList.listener;

import java.io.File;

import javax.swing.ButtonGroup;
import javax.swing.JMenu;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

import org.sny7.openOfflineList.gui.LanguageMenuItem;
import org.sny7.openOfflineList.util.LanguageChanger;
import org.sny7.openOfflineList.util.PathGetter;
import org.sny7.openOfflineList.util.PropertiesFilenameFilter;

public class LanguageMenuClick implements MenuListener {
	
	public void menuCanceled(MenuEvent e) {
		
	}

	public void menuDeselected(MenuEvent e) {
		
	}

	public void menuSelected(MenuEvent e) {
		JMenu languageMenu = (JMenu) e.getSource();
		
		ButtonGroup languageGroup = new ButtonGroup();
		
		File languageFilesPath = new File(PathGetter.getPath() + "languages");
		if (languageFilesPath == null) {
			return;
		}
		File[] languageFiles = languageFilesPath.listFiles(new PropertiesFilenameFilter());
		
		String currentLanguageName = LanguageChanger.getCurrentLanguage().replaceFirst("\\..+", "").replaceFirst("/.*/", "");
		for (File languageFile : languageFiles) {
			String languageName = languageFile.getName().replaceFirst("\\..+", "");
			LanguageMenuItem languageMenuItem = new LanguageMenuItem();
			languageMenuItem.setFileName(languageFile.getPath());
			languageMenuItem.setText(languageName);
			if (currentLanguageName.equals(languageName)){
				languageMenuItem.setSelected(true);
			}
			languageGroup.add(languageMenuItem);
			languageMenu.add(languageMenuItem);
		}
		
		languageMenu.removeMenuListener(this);
	}

}
